function SysConfigController(){
	var logoText = $("#tempo-logo-text-div");
	var userSwitchButton = $("#_switchUserDepartment"); 
	var tempoHeaderNav = $("#tempo-header-nav"); 
	var tempoSystemToolBar = $("#tempo-system-toobar"); 
	var loginQrcode = $("#empo-login-qrcode");
	this.init = function(){
		initLoginText();
		closeShortCut();
		// loadUserOrgs();
		bindUserSwitchButtonEvent();
		initLoginQrcode();
		showMsgConfig.loadWarningInfo();
		if(!!window.apiDesigner){
			var hash = self.location.hash;
			try {
				hash = top.location.hash;
			} catch(e) {
			}
		   if(hash == "#bi"){
				initThemePath(subjectId,"visual","/api/bi/theme/path","/api/bi/resource/tree/list-own-analyse","/api/bi/resource/analyse/view/index?id=");
			}else{
				if(window.themeid && window.themeid.length>0){
					// var themeid = window.themeid
					// initThemePath(themeid,"mining","/api/ai/resource/analyse/path","/api/ai/resource/analyse/tree","/api/ai/resource/analyse/view/index?id=");
				}
			}
		}
	}
	var initThemePath=function(subjectid,type,url_path,tree_url,index_url){
		$.ajax({
			url:contextPath+url_path, //当前主题路径
			data:{
				"id": subjectid
			},
			type:'get',
			async:false,
			success:function(result){
				if(result.success){
					var data=result.data;
					var navData=[];
				 		if(data.workspace!==null){
				 			navData.push({
								title : data["workspace"].name,
								id:data["workspace"].id,
								type : "text",
								click : function(data){
									// top.$(".tempo-theme-detail").dialog('close')
									var workSpaceId = checkUrlParam(data.id);
									var thisUrl = contextPath + "/api/ai/resource/view/theme?workSpaceId=" + workSpaceId;
									thisUrl = TempoUtils.addHideIconParam(thisUrl);
									window.location.href = thisUrl;
									return false;
								}
							})
				 		}
						if(data.root!==null){
							navData.push({
								title : data["root"].name,
								type : "text",
								click : function(data){
									top.$(".tempo-theme-detail").dialog('close')
									return false;
								}
							})
						}
						if(data.folder!==null){
							navData.push({
								title : data["folder"].name,
								type : "text",
								data :data["folder"],
								click : function(data){
									top.$(".tempo-theme-detail").dialog('close')
									return false;
								}
							})
						}
						if(data.theme!==null){
							var newName = TempoUtils.locale("bi.new_visual")
							if(type == "mining"){
								newName = TempoUtils.locale("bi.new_visual")
							}
							navData.push({
								title : data["theme"].name,
								id:data["theme"].id,
								type :"select",
								option : {
									listUrl : contextPath + tree_url,//加载当前目录下所有主题信息
									type : "mining",
									createNewResourceItem:false,
									parentId : data["theme"].folderId,
									workSpaceId:data["theme"].workSpaceId,
									notShow:TempoAuthorityUtils.isDesignerLimited(),
									newName:newName,
									itemClick:function(data){
										debugger
										var id = data.ID || data.id;
										var url = index_url + id; //加载主题页
										var thisUrl = contextPath+url;
										thisUrl = TempoUtils.addHideIconParam(thisUrl);
										window.location.href = thisUrl;
									}
								}
							})
						}
					$('<span class="tempo-theme-path-icon" title="当前位置"></span>').appendTo($(".tempo-theme-path"));
					$("<div>").navigation({
						data:navData,
					}).appendTo($(".tempo-theme-path"));
				}
			}
		})
	}


	var checkUrlParam = function(param){
		var regExp = new RegExp(/^[0-9a-zA-z]*$/);
		var params = param.toString()
		if(regExp.test(params)){
			return params;
		}else {
			return "";
		}
	}
	
	var bindUserSwitchButtonEvent = function(){
		userSwitchButton.unbind("click").bind("click",function(e){
			$("#tempo-system-user-org-switch-menu").contextmenu("show",{
				position:{
					left:e.pageX - 80,
					top:e.pageY
				},
				linkEl:this
			});
		});
	}
	
	var initLoginText = function() {
		if ("ai" == "ai") {
			// 人工智能版本的修改点
			// 1 修改右上角标题 2 屏蔽帮助功能
			logoText.text(TempoUtils.locale("sysheader.ailogotext"));
			$("#help_doc").hide();
		} else {
			logoText.text(TempoUtils.locale("sysheader.logotext"));
		}
	}
	
	var initLoginQrcode = function(){
		var qrcodeCon = $("body").find("#tempo-header-qrcode-container");
		var qrcodediv;
		if(qrcodeCon.length == 0){
			qrcodeCon = $("<div/>",{
				id: "tempo-header-qrcode-container"
			}).hide();
			qrcodediv = $("<div/>",{
				id: "tempo-header-qrcode-div",
				style: "padding:20px;width:120px;height:120px"
			}).appendTo(qrcodeCon);
			qrcodeCon.appendTo($("body"));
			var render = 'canvas';
			if(!document.createElement("canvas").getContext){
				render = "table";
			}
			qrcodediv.qrcode({
				render:render,
				text:encodeURI(location.protocol+"//"+location.host+contextPath)+"/login.jsp?qrcode=true",
				width:120,
				height:120,
				correctLevel:0
			});
		}

		$(document).tooltip({
			items:'#tempo-login-qrcode',
			content:function(){
				return qrcodediv;
			}
		});
	} 
	
	/**
	 * 加载当前用户机构信息
	 */
	var loadUserOrgs = function(){
		$.ajax({
			url:contextPath + '/userswitch/loadUserOrgs',
			type:'post',
			success:function(resultData){
				tempoHeaderNav.css({
					width:'150px'
				});
				var isAllow = isAllowToSwitchOrg();
				initUserSwitchMenu(resultData.orgs,resultData.selectedId, isAllow);
			}
		});
	}
	
	/**
	 * 分析主题、可视化设计、挖掘分析，其它页面返回当前页
	 */
	var isAllowToSwitchOrg = function(){
		var path = window.location.pathname;
		if(path == contextPath + '/'){
			path += "index.jsp";
		}
		if(path.indexOf("/project.jsp") >= 0 
				|| path.indexOf("/index.jsp") >= 0 
					|| path.indexOf("/dataview.jsp") >= 0
					|| path.indexOf("/cggl.jsp") >= 0){
			return true;
		}
		return false;
	}
	/**
	 * 初始化菜单
	 */
	var initUserSwitchMenu = function(orgsInfo, selectId){
		var data = [];
		var selectName = "";
		for(var org in orgsInfo){
			var orgItem = orgsInfo[org];
			if(selectId == orgItem.id){
				selectName = orgItem.name;
			}
			data.push({id : org, name : orgItem.name});
		}
		//如果只有一个组织机构，
		var isEnable = isAllowToSwitchOrg() && data.length > 1;
		
		if(isEnable) {
			tempoHeaderNav.navigation({
				data:[{
					id : selectId,
					title :selectName,
					type : "select",
					data :　data,
					click :$.noop,
					option :　{
						newName:'sdfdsf',
						notShow:true,
						itemClick:function(paramData){
							switchOrg(paramData.id);
						}
					}
				}]
			})
		} else {
			tempoHeaderNav.text(selectName);
		}
	}
	
	
	/**
	 * 
	 */
	var switchOrg = function(targetOrgId){
		$.ajax({
			url:contextPath + '/userswitch/switchorg',
			data:{
				orgId:targetOrgId
			},
			type:"post",
			success:function(resultData){
				window.location.reload()
			}
		});
	}
	
	
	
	
	
	var closeShortCut = function(){
		$("#tempo-shortcut").bind("click",function(){
			Eventbus.trigger("toggle.sysshortcut"); 
		});
	}
	
	/**
	 * 在此加载系统切换菜单的权限
	 */
	var loadSystemMenus = function(){
		$.ajax({
			url:contextPath + '/sysmenu/loadLocationMenus',
			type:'post',
			success:function(data){
				data = canculateData(data);
				 $("<ul/>",{
					id:'tempo-system-location-menu'
				}).appendTo($("body")).contextmenu({
					data: data,
					select:function(e,ui){
						  var link = ui.linkEl;
						  var itemId = ui.select.id;
						  if(itemId == 'changePassWord'){
								Eventbus.trigger("changePassEvent.main");
						  }else{
							  window.location.href = contextPath + itemId;
						  }
					 }
				});
			}
		});
		
		/**
		 * 如果是当前页，则需要将本菜单去掉
		 */
		var canculateData = function(data){
			data.push({
				id:'changePassWord',
				icon:'system-location-menu-password',
				text:TempoUtils.locale("sysheader.changepassword")
			});
			var reg = /^#$/g;
			var url = window.location.href;
			url = url.substring(url.lastIndexOf("/") + 1);
			if(url.indexOf("project.jsp") >= 0){
				// 进入某个工程
				for(var i =0;i<data.length;i++){
					if(data[i].id == "/project.jsp"){
						data.splice(i,1);
					}
				}
			}else if( !url || url.indexOf("index.jsp") >= 0 || reg.test(url)){
				// 进入我的设计
				for(var i =0;i<data.length;i++){
					if(data[i].id == "/index.jsp"){
						data.splice(i,1);
					}
				}
			}else if(url.indexOf("dataview.jsp") >= 0){
				// 进入我的数据
				for(var i =0;i<data.length;i++){
					if(data[i].id == "/dataview.jsp"){
						data.splice(i,1);
					}
				}
			}
			
			return data;
		}
	}
	
	var showMsgConfig = {
			loadWarningInfo:function(){
				/*var warinFlag = $.cookie("tempo_warning_close");
				if(warinFlag !== "true"){
					$.ajax({
						url: contextPath+'/sysindicator/showWarn',
						type:'post',
						success:function(result){
							if(result && result.length > 0){
								$("<div />").msgalert({
									width:400,
									height:280,
									data:result,
									onClose:function(){
										$.cookie("tempo_warning_close","true");
									}
								})
							}
							
						}
					});
				}*/
			}
		}
}



$(function(){
	if(TempoUtils.browser.isIE8) {
		$("#tempo-shortcut").hide();
		$('#help_doc').hide();
	}
	new SysConfigController().init();
	
})